#include "ServerData.h"
#include "../../../ThirdParty/sqlite3.6.11/src/CppSQLite3.h"
#include "../../../ThirdParty/tinyxml-2.5.3/tinyxml.h"
#include "../../../Components/Common/LibUtil/libutil.h"
#include "../../../Components/Common/LibCommon/int128.h"
#include "../../../Components/Common/LibCommon/util.h"
#include <string>
#include <deque>
#include <map>
#include <algorithm>
using namespace std;
#include <direct.h>
#include <cctype>
#include "../ParseServer/XmlTag.h"
#include "../ParseServer/status.h"

USING_LIB_UTIL
#define WtoUTF8(x) WideCharToUTF8(wstring(x))
#define UTF8toW(x) UTF8ToWideChar(string(x))

static const char TABLE_SHAREFOLDER[]	="TABSHAREFOLDER";
static const char TABLE_MEDIA[]			="TABMEDIA";

// Global items
const char *			g_XMLData = NULL;
TiXmlDocument *			g_XmlDoc = NULL;
string					g_XmlDocType;
ShareFolderListXML		g_ShareFolderList;
ShareFolderXML			g_ShareFolder;
ServerStatusXML			g_ServerStatus;

int LoadShareFolderListXML();
int LoadShareFolderXML();
int LoadServerStatusXML();

int ResetXMLData()
{
	if (g_XmlDoc)
		delete g_XmlDoc;
	g_ShareFolderList.Reset();
	g_ShareFolder.Reset();
	g_XmlDocType.clear();
	return 0;
}

int SetXMLData(const char * xmlbuf)
{
	ResetXMLData();
	g_XMLData = xmlbuf;
	g_XmlDoc = new TiXmlDocument();
	const char * xml = g_XmlDoc->Parse(xmlbuf);
	TiXmlElement * Root = g_XmlDoc->FirstChildElement();
	if (!Root) return -1;
	g_XmlDocType = Root->Value();
	if (g_XmlDocType == "ShareFolderList")
	{
		LoadShareFolderListXML();
	}
	else if (g_XmlDocType == "ShareFolder")
	{
		LoadShareFolderXML();
	}
	else if (g_XmlDocType == "ServerStatus")
	{
		LoadServerStatusXML();
	}
	return 0;
}

int LoadShareFolderListXML()
{
	g_ShareFolderList.Reset();
	g_ShareFolderList.Load(g_XMLData);
	return 0;
}

int LoadShareFolderXML()
{
	g_ShareFolder.Reset();
	g_ShareFolder.Load(g_XMLData);
	return 0;
}

int LoadServerStatusXML()
{
	g_ServerStatus.Reset();
	g_ServerStatus.Load(g_XMLData);
	return 0;
}

int GetShareFolderListCount(int* Count)
{
	*Count = 0;
	// Check whether the xml file is ShareFolderList
	if (g_XmlDocType != "ShareFolderList")
	{
		return -1;
	}
	*Count = g_ShareFolderList.List.size();
	return 0;
}

int GetShareFolderListItem(int Index,SERVERDATA_SHAREDFOLDER* pSharedFolder)
{
	// Check whether the xml file is ShareFolderList
	if (g_XmlDocType != "ShareFolderList")
		return -1;
	if (Index >= g_ShareFolderList.List.size())
		return -2;
	_itow(Index, pSharedFolder->id, 10);
	lstrcpy(pSharedFolder->szPath, g_ShareFolderList.List[Index].path.c_str());
	return 0;
}

int GetShareItemCount(int* Count)
{
	*Count = 0;
	if (g_XmlDocType != "ShareFolder")
	{
		return -1;
	}
	*Count = g_ShareFolder.ShareItems.size();
	return 0;
}

int GetShareItemInfo(int Index,SERVERDATA_MEDIA* pMedia)
{
	// Check whether the xml file is ShareFolderList
	if (g_XmlDocType != "ShareFolder")
		return -1;
	if (Index >= g_ShareFolder.ShareItems.size())
		return -2;
	_itow(Index, pMedia->id, 10);
	pMedia->isFolder = (g_ShareFolder.ShareItems[Index].type == "folder");
	lstrcpy(pMedia->szName, g_ShareFolder.ShareItems[Index].name.c_str());
	lstrcpy(pMedia->szPath, g_ShareFolder.ShareItems[Index].path.c_str());
	string& filetype = g_ShareFolder.ShareItems[Index].filetype;
	if ( filetype == FILE_TYPE_AUDIO)
	{
		pMedia->FileType = FileType_Audio;
	}
	else if (filetype == FILE_TYPE_VIDEO)
	{
		pMedia->FileType = FileType_Video;
	}
	else if (filetype == FILE_TYPE_DOCUMENT)
	{
		pMedia->FileType = FileType_Document;
	}
	else if (filetype == FILE_TYPE_BINARY)
	{
		pMedia->FileType = FileType_Binary;
	}
	else if (filetype == FILE_TYPE_HTML)
	{
		pMedia->FileType = FileType_HTML;
	}
	return 0;	
}

wstring Lower(wstring str)
{
	transform(str.begin(),str.end(),str.begin(),std::tolower);
	return str;
};

class CServerXmlHelper
{
public:
	CServerXmlHelper(TCHAR* dbPath)
	{
		m_Document = new TiXmlDocument();
		m_SavePath = WtoUTF8(dbPath);
		m_Document->LoadFile(m_SavePath.c_str());
		TiXmlElement* rootElement = m_Document->RootElement();  //Root
		if (rootElement == NULL)
		{
			 rootElement = new TiXmlElement("ServerSharedInfomation");
			 m_Document->LinkEndChild(rootElement);
		}
		TiXmlElement* FoldersElement = rootElement->FirstChildElement();	//SharedFolders
		if (FoldersElement == NULL)
		{
			 FoldersElement = new TiXmlElement("SharedFolders");
			 rootElement->LinkEndChild(FoldersElement);
		}
		TiXmlElement* FolderElement = FoldersElement->FirstChildElement();  //Folder
		while ( FolderElement ) 
		{
			string strElement = FolderElement->Value();
			if (strElement == "Folder")
			{
				SERVERDATA_SHAREDFOLDER* SharedFolder = new SERVERDATA_SHAREDFOLDER();
				_tcscpy_s(SharedFolder->id,MAX_PATH,UTF8toW(FolderElement->Attribute("id")).c_str());
				_tcscpy_s(SharedFolder->szPath ,MAX_PATH, UTF8toW(FolderElement->Attribute("path")).c_str());

				m_SharedFolders[Lower((wstring)SharedFolder->szPath)] = SharedFolder;
			}
			else if (strElement == "Medias")
			{
				wstring SharedFolderPath = UTF8toW(FolderElement->Attribute("SharedFolderPath"));
				SHAREMEDIAS_ONEFOLDER* SharedMap = new SHAREMEDIAS_ONEFOLDER();
				m_SharedeMedias[Lower(SharedFolderPath)] = SharedMap;
				TiXmlElement* MediaElement = FolderElement->FirstChildElement();  //Medias
				while ( MediaElement ) 
				{
					SERVERDATA_MEDIA* SharedMedia = new SERVERDATA_MEDIA();
					string attrib = MediaElement->Attribute("id");
					_tcscpy_s(SharedMedia->id,MAX_PATH,UTF8toW(attrib).c_str());
					char szIsFolder[MAX_PATH] = {0};
					attrib = MediaElement->Attribute("isfolder");
					strcpy_s(szIsFolder,MAX_PATH,attrib.c_str());
					SharedMedia->isFolder = _strtoui64(szIsFolder, NULL, 10);

					//_tcscpy_s(SharedMedia->ShareFolderPath ,MAX_PATH, SharedFolderPath.c_str());
					attrib = MediaElement->Attribute("name");
					_tcscpy_s(SharedMedia->szName ,MAX_PATH, UTF8toW(attrib).c_str());
					attrib = MediaElement->Attribute("path");
					_tcscpy_s(SharedMedia->szPath ,MAX_PATH, UTF8toW(attrib).c_str());
					attrib = MediaElement->Attribute("thumbpath");
					//_tcscpy_s(SharedMedia->szThumbPath ,MAX_PATH, UTF8toW(attrib).c_str());

					(*SharedMap)[Lower((wstring)SharedMedia->szPath)] = SharedMedia;

					MediaElement = MediaElement->NextSiblingElement();
				}
			}

			FolderElement = FolderElement->NextSiblingElement();
		}
	}
	~CServerXmlHelper()
	{
		//release map data. do not done
		Save();
	}
	void Save()
	{
		TiXmlElement* rootElement = m_Document->RootElement();  //Root
		if (rootElement == NULL)
		{
			 rootElement = new TiXmlElement("ServerSharedInfomation");
			 m_Document->LinkEndChild(rootElement);
		}
		TiXmlElement* FoldersElement = rootElement->FirstChildElement();	//SharedFolders
		if (FoldersElement == NULL)
		{
			 FoldersElement = new TiXmlElement("SharedFolders");
			 rootElement->LinkEndChild(FoldersElement);
		}
		FoldersElement->Clear();
		SHAREFOLDERS::iterator it = m_SharedFolders.begin();
		int i = 0;
		while( it != m_SharedFolders.end())
		{
			 TiXmlElement* FolderElement = new TiXmlElement("Folder");
			 FolderElement->SetAttribute("id",(WtoUTF8(it->second->id)).c_str());
			 FolderElement->SetAttribute("path",(WtoUTF8(it->second->szPath)).c_str());
			 FoldersElement->LinkEndChild(FolderElement);
			it++;
		}

		SHAREMEDIAS::iterator itfolder = m_SharedeMedias.begin();
		while( itfolder != m_SharedeMedias.end())
		{
			 TiXmlElement* MediasElement = new TiXmlElement("Medias");
			 MediasElement->SetAttribute("SharedFolderPath",(WtoUTF8(itfolder->first)).c_str());
			 FoldersElement->LinkEndChild(MediasElement);

			 SHAREMEDIAS_ONEFOLDER* Medias = itfolder->second;
			 SHAREMEDIAS_ONEFOLDER::iterator itMedia = (*Medias).begin();
			 while( itMedia != (*Medias).end())
			 {
				 TiXmlElement* MediaElement = new TiXmlElement("Media");
				 MediaElement->SetAttribute("id",(WtoUTF8(itMedia->second->id)).c_str());
				 //MediaElement->SetAttribute("sharefolder",(WtoUTF8(itMedia->second->ShareFolderPath)).c_str());
				 MediaElement->SetAttribute("name",(WtoUTF8(itMedia->second->szName)).c_str());
				 MediaElement->SetAttribute("path",(WtoUTF8(itMedia->second->szPath)).c_str());
				 //MediaElement->SetAttribute("thumbpath",(WtoUTF8(itMedia->second->szThumbPath)).c_str());
				 char szisFolder[MAX_PATH] = {0};
				 itoa(itMedia->second->isFolder,szisFolder,10);
				 MediaElement->SetAttribute("isfolder",szisFolder);

				 MediasElement->LinkEndChild(MediaElement);

				 itMedia++;
			 }
			 itfolder++;
		}
		m_Document->SaveFile();
	}
	typedef map<wstring,SERVERDATA_SHAREDFOLDER*> SHAREFOLDERS;
	typedef map<wstring,SERVERDATA_MEDIA*>		  SHAREMEDIAS_ONEFOLDER;
	typedef map<wstring,SHAREMEDIAS_ONEFOLDER*>	  SHAREMEDIAS;

	int GetShareFoldersCount(int* Count)
	{
		if (Count == NULL)
			return 0;
		*Count = m_SharedFolders.size();
		return 0;
	}
	int GetShareFolder(int Index,SERVERDATA_SHAREDFOLDER* pSharedFolder)
	{
		if (pSharedFolder == NULL)
			return 0;
		SHAREFOLDERS::iterator it = m_SharedFolders.begin();
		int i = 0;
		while( it != m_SharedFolders.end())
		{
			if ( i == Index)
			{
				*pSharedFolder = *it->second;
				return 0;
			}
			it++;i++;
		}
		return 0;
	}

	int GetShareFolderMediasCount(TCHAR* szShareFolderPath,int* Count)
	{
		if (szShareFolderPath == NULL || Count == NULL)
			return 0;
		SHAREMEDIAS_ONEFOLDER* Medias = m_SharedeMedias[Lower((wstring)szShareFolderPath)];
		if (Medias == NULL )
			return 0;
		*Count = Medias->size();
		return 0;
	}
	int GetShareFolderMediasInfo(TCHAR* szShareFolderPath,int nIndex,SERVERDATA_MEDIA* pMedia)
	{
		if (szShareFolderPath == NULL)
			return 0;
		SHAREMEDIAS_ONEFOLDER* Medias = m_SharedeMedias[Lower((wstring)szShareFolderPath)];
		if (Medias == NULL )
			return 0;
		SHAREMEDIAS_ONEFOLDER::iterator it = Medias->begin();
		int i = 0;
		while( it != Medias->end())
		{
			if ( i == nIndex)
			{
				*pMedia = *it->second;
				return 0;
			}
			it++;i++;
		}
		return 0;
	}

	int AddShareFolder(TCHAR* pPath)
	{
		string strSQL;
		string strFormat;
		char szpath[MAX_PATH] = {0};
		wstring lowerwPath = pPath;
		lowerwPath = Lower(lowerwPath);
		WideCharToMultiByte(CP_UTF8, 0, lowerwPath.c_str(), -1, szpath, _MAX_PATH, NULL, NULL);
		string filepatha = szpath;
		size_t npos = filepatha.rfind('\\');
		npos++;
		string path = filepatha.substr(0,npos);
		string name = filepatha.substr(npos,filepatha.length()-npos);
		tDataBuffer buffer(true);
		buffer.size = strlen(szpath);
		buffer.data = (unsigned char*)szpath;
		char* szHash = MD5HashHelper::GetDataBufferHashString(buffer);

		SERVERDATA_SHAREDFOLDER* SharedFolder = new SERVERDATA_SHAREDFOLDER();
		_tcscpy_s(SharedFolder->id,MAX_PATH,UTF8toW((string)szHash).c_str());
		_tcscpy_s(SharedFolder->szPath ,MAX_PATH, pPath);

		m_SharedFolders[Lower((wstring)SharedFolder->szPath)] = SharedFolder;

		MD5HashHelper::ReleaseHashString(szHash);

		Save();
		return 0;
	}
	int RemoveShareFolder(TCHAR* pPath)
	{
		if (pPath == NULL)
			return 0;
		wstring path = Lower((wstring)pPath);
		SHAREFOLDERS::iterator it = m_SharedFolders.begin();
		int i = 0;
		while( it != m_SharedFolders.end())
		{
			if ( path == Lower(it->second->szPath))
			{
				m_SharedFolders.erase(it);
				return 0;
			}
			it++;i++;
		}
		return 0;
	}

	int AddSharedMediaInfo(SERVERDATA_MEDIA* pMedia)
	{
		/*
		if (pMedia == NULL)
			return 0;
		SHAREMEDIAS_ONEFOLDER* Medias = m_SharedeMedias[Lower((wstring)pMedia->ShareFolderPath)];
		if (Medias == NULL )
		{
			Medias = new SHAREMEDIAS_ONEFOLDER();
			m_SharedeMedias[Lower((wstring)pMedia->ShareFolderPath)] = Medias;
		}
		if ((*Medias)[Lower(pMedia->szPath)] == NULL)
		{
			SERVERDATA_MEDIA* newMedia = new SERVERDATA_MEDIA();
			*newMedia = *pMedia;
			(*Medias)[Lower(newMedia->szPath)] = newMedia;
		}
		*/
		return 0;
	}
	int RemoveSharedMediaInfo(SERVERDATA_MEDIA* pMedia)
	{
		/*
		if (pMedia == NULL)
			return 0;
		SHAREMEDIAS_ONEFOLDER* Medias = m_SharedeMedias[Lower((wstring)pMedia->ShareFolderPath)];
		if (Medias == NULL )
		{
			return 0;
		}
		SHAREMEDIAS_ONEFOLDER::iterator it = (*Medias).find(Lower(pMedia->szPath));
		if (it != (*Medias).end())
		{
			(*Medias).erase(it);
		}
		*/
		return 0;
	}
	TiXmlDocument*		m_Document;
	SHAREFOLDERS		m_SharedFolders;
	SHAREMEDIAS			m_SharedeMedias;
	string				m_SavePath;
};

/*
class CServerDBHelper
{
public:
	CServerDBHelper(TCHAR* dbPath)
	{
		char path[_MAX_PATH] = {0};
		WideCharToMultiByte(CP_UTF8, 0, (LPCTSTR)dbPath, -1, path, _MAX_PATH, NULL, NULL);
		m_bDatabaseReady = false;
		m_dbfile = path;

		if	( CreateDB() !=SQLITE_OK)   
			return;

		m_bDatabaseReady = true;
	}
	~CServerDBHelper()
	{
		m_SQLiteDB.close();
	}
	int	CreateDB()
	{
		int resultCode = SQLITE_OK;
		std::string::iterator it_slash = m_dbfile.begin();
		while (it_slash != m_dbfile.end())
		{
			it_slash = std::find(it_slash, m_dbfile.end(), L'\\');
			if (it_slash != m_dbfile.end())
			{
				std::string dir = m_dbfile.substr(0, it_slash - m_dbfile.begin());
				_mkdir(dir.c_str());
				it_slash++;
			}
		}
		m_SQLiteDB.open(m_dbfile.c_str());
		string strSQL;
		strSQL = "CREATE TABLE IF NOT EXISTS TABSHAREFOLDER( \
			  		SHAREFOLDER_ID TEXT PRIMARY KEY, \
					SHAREFOLDER_PATH TEXT);";
		if (!m_SQLiteDB.tableExists(TABLE_SHAREFOLDER))
		resultCode = m_SQLiteDB.execDML(strSQL.c_str());
		strSQL = "CREATE TABLE IF NOT EXISTS TABMEDIA( \
		 		MEDIA_ID TEXT PRIMARY KEY, \
		 		SHAREFOLDER_PATH TEXT,\
				MEDIA_PATH TEXT,\
				MEDIA_THUMBPATH TEXT,\
				MEDIA_NAME TEXT,\
				IS_FOLDER INTEGER);";
		if (!m_SQLiteDB.tableExists(TABLE_MEDIA))
		resultCode = m_SQLiteDB.execDML(strSQL.c_str());

		return resultCode;
	}
	int GetShareFoldersCount(int* Count)
	{
		if (Count == NULL)
			return 0;
		CppSQLite3Buffer bf;
		string strSQL=bf.format("SELECT COUNT(*) FROM TABSHAREFOLDER");
		int cout=m_SQLiteDB.execScalar(strSQL.c_str());
		*Count = cout;

		return 0;
	}
	int GetShareFolder(int Index,SERVERDATA_SHAREDFOLDER* pSharedFolder)
	{
		if (pSharedFolder == NULL)
			return 0;
		string query = "";
		CppSQLite3Buffer bf;
		string strSQL=bf.format("SELECT * FROM TABSHAREFOLDER DESC limit %d,%d;",Index,Index+1);

		CppSQLite3Query q = m_SQLiteDB.execQuery(strSQL.c_str());
		while (!q.eof())
		{
			string folderID		=string(q.fieldValue(0));
			string folderpath	=string(q.fieldValue(1));
			_tcscpy_s(pSharedFolder->id,sizeof(pSharedFolder->id)/sizeof(pSharedFolder->id[0]),UTF8toW(folderID).c_str() );
			_tcscpy_s(pSharedFolder->szPath,sizeof(pSharedFolder->szPath)/sizeof(pSharedFolder->szPath[0]),UTF8toW(folderpath).c_str() );

			q.nextRow();
		}
		bf.clear();
		
		return 0;
	}
	int AddShareFolder(TCHAR* pPath)
	{
		CppSQLite3Buffer bf; //if it 16 format, use %#llx,else use %lld
		string strSQL;
		string strFormat;
		char szpath[MAX_PATH] = {0};
		wstring lowerwPath = pPath;
		lowerwPath = Lower(lowerwPath);
		WideCharToMultiByte(CP_UTF8, 0, lowerwPath.c_str(), -1, szpath, _MAX_PATH, NULL, NULL);
		string filepatha = szpath;
		size_t npos = filepatha.rfind('\\');
		npos++;
		string path = filepatha.substr(0,npos);
		string name = filepatha.substr(npos,filepatha.length()-npos);
		tDataBuffer buffer(true);
		buffer.size = strlen(szpath);
		buffer.data = (unsigned char*)szpath;
		char* szHash = MD5HashHelper::GetDataBufferHashString(buffer);

		WideCharToMultiByte(CP_UTF8, 0, pPath, -1, szpath, _MAX_PATH, NULL, NULL);

		strFormat = "INSERT INTO TABSHAREFOLDER (SHAREFOLDER_ID,SHAREFOLDER_PATH)\
						VALUES('%q', '%q');";
		strSQL = bf.format(strFormat.c_str(),szHash,szpath);

		int resultCode = m_SQLiteDB.execDML(strSQL.c_str());
		bf.clear();
		MD5HashHelper::ReleaseHashString(szHash);
		return resultCode;
	}
	int RemoveShareFolder(TCHAR* pPath)
	{
		int resultCode = SQLITE_ERROR;
		string path = WtoUTF8(Lower(pPath));
		tDataBuffer buffer(true);
		buffer.size = strlen(path.c_str());
		buffer.data = (unsigned char*)path.c_str();
		char* szHash = MD5HashHelper::GetDataBufferHashString(buffer);

		CppSQLite3Buffer bf; 
		string strSQL = bf.format("DELETE FROM TABSHAREFOLDER WHERE SHAREFOLDER_ID = '%q';", szHash);
		resultCode = m_SQLiteDB.execDML(strSQL.c_str());
		bf.clear();

		MD5HashHelper::ReleaseHashString(szHash);
		return resultCode;
	}
	int GetShareFolderMediasCount(TCHAR* szShareFolderPath,int* Count)
	{
		if (szShareFolderPath == NULL || Count == NULL)
			return 0;
		CppSQLite3Buffer bf;
		string strSQL=bf.format("SELECT COUNT(*) FROM TABMEDIA");
		int cout=m_SQLiteDB.execScalar(strSQL.c_str());
		
		return cout;
	}
	int GetShareFolderMediasInfo(TCHAR* szShareFolderPath,int Index,SERVERDATA_MEDIA* pMedia)
	{
		if (pMedia == NULL)
			return 0;
		string query = "";
		CppSQLite3Buffer bf;
		string strSQL=bf.format("SELECT MEDIA_ID,SHAREFOLDER_PATH,MEDIA_PATH,MEDIA_THUMBPATH,MEDIA_NAME,IS_FOLDER FROM TABMEDIA DESC limit %d,%d;",Index,Index+1);

		CppSQLite3Query q = m_SQLiteDB.execQuery(strSQL.c_str());
		while (!q.eof())
		{
			string mediaID		= string(q.fieldValue(0));
			string folderpath	= string(q.fieldValue(1));
			string mediapath	= string(q.fieldValue(2));
			string thumbpath	= string(q.fieldValue(3));
			string name			= string(q.fieldValue(4));
			string isfolder		= string(q.fieldValue(5));
			int bfolder			= _strtoui64(isfolder.c_str(), NULL, 10);

			_tcscpy_s(pMedia->id,MAX_PATH,UTF8toW(mediaID).c_str());
			pMedia->isFolder = bfolder;
			_tcscpy_s(pMedia->ShareFolderPath,MAX_PATH,UTF8toW(folderpath).c_str());
			_tcscpy_s(pMedia->szPath,MAX_PATH,UTF8toW(mediapath).c_str());
			_tcscpy_s(pMedia->szThumbPath,MAX_PATH,UTF8toW(thumbpath).c_str());
			_tcscpy_s(pMedia->szName,MAX_PATH,UTF8toW(name).c_str());

			q.nextRow();
		}
		bf.clear();
		
		return 0;
	}
	int AddSharedMediaInfo(SERVERDATA_MEDIA* pMedia)
	{
		if (pMedia == NULL)
			return 0;
		CppSQLite3Buffer bf; //if it 16 format, use %#llx,else use %lld
		string strSQL;
		string strFormat;
		char szpath[MAX_PATH] = {0};
		wstring lowerwPath = pMedia->szPath;
		lowerwPath = Lower(lowerwPath);
		WideCharToMultiByte(CP_UTF8, 0, lowerwPath.c_str(), -1, szpath, _MAX_PATH, NULL, NULL);

		tDataBuffer buffer(true);
		buffer.size = strlen(szpath);
		buffer.data = (unsigned char*)szpath;
		char* szHash = MD5HashHelper::GetDataBufferHashString(buffer);
		WideCharToMultiByte(CP_UTF8, 0, pMedia->szPath, -1, szpath, _MAX_PATH, NULL, NULL);


		strFormat = "INSERT INTO TABMEDIA (MEDIA_ID,SHAREFOLDER_PATH,MEDIA_PATH,MEDIA_THUMBPATH,MEDIA_NAME,IS_FOLDER)\
						VALUES('%q', '%q','%q','%q','%q', '%d');";
		strSQL = bf.format(strFormat.c_str(),szHash,WtoUTF8((wstring)pMedia->ShareFolderPath).c_str(),szpath,WtoUTF8((wstring)pMedia->szThumbPath).c_str(), WtoUTF8((wstring)pMedia->szName).c_str(), pMedia->isFolder);

		int resultCode = m_SQLiteDB.execDML(strSQL.c_str());
		bf.clear();
		MD5HashHelper::ReleaseHashString(szHash);

		return resultCode;
	}
	int RemoveSharedMediaInfo(SERVERDATA_MEDIA* pMedia)
	{
		int resultCode = SQLITE_ERROR;
		string path = WtoUTF8(Lower(pMedia->szPath));
		tDataBuffer buffer(true);
		buffer.size = strlen(path.c_str());
		buffer.data = (unsigned char*)path.c_str();
		char* szHash = MD5HashHelper::GetDataBufferHashString(buffer);

		CppSQLite3Buffer bf; 
		string strSQL = bf.format("DELETE FROM TABMEDIA WHERE MEDIA_ID = '%q';", szHash);
		resultCode = m_SQLiteDB.execDML(strSQL.c_str());
		bf.clear();

		MD5HashHelper::ReleaseHashString(szHash);
		return resultCode;
	}

	CppSQLite3DB	m_SQLiteDB;
	BOOL			m_bDatabaseReady;
	string			m_dbfile;

};
#ifdef _DATABASE
CServerDBHelper* Helper = NULL;
#else
CServerXmlHelper* Helper = NULL;
#endif 

#ifdef _cplusplus
extern "C"
{
#endif





int InitServerData(TCHAR* pszDataBasePath)
{
#ifdef _DATABASE
	Helper = new CServerDBHelper(pszDataBasePath);
#else
	Helper = new CServerXmlHelper(pszDataBasePath);
#endif

	return 0;
}
int CloseServerData()
{
	if (Helper)
		delete Helper;
	Helper = NULL;
	return 0;
}

int GetShareFolderCount(TCHAR* szShareFolderPath,int* Count)
{
	return 0;
}
int GetShareFolderMediasInfo(TCHAR* szShareFolderPath,int nIndex,SERVERDATA_MEDIA* pMedia)
{
	if (Helper)
		Helper->GetShareFolderMediasInfo(szShareFolderPath,nIndex,pMedia);
	return 0;
}

int AddShareFolder(TCHAR* pPath)
{
	if (Helper)
		Helper->AddShareFolder(pPath);
	return 0;
}
int RemoveShareFolder(TCHAR* pPath)
{
	if (Helper)
		Helper->RemoveShareFolder(pPath);
	return 0;
}
int AddSharedMediaInfo(SERVERDATA_MEDIA* pMedia)
{
	if (Helper)
		Helper->AddSharedMediaInfo(pMedia);
	return 0;
}
int RemoveSharedMediaInfo(SERVERDATA_MEDIA* pMedia)
{
	if (Helper)
		Helper->RemoveSharedMediaInfo(pMedia);
	return 0;
}

#ifdef _cplusplus
}

#endif
*/